package com.gitee.melin.bee.core.jdbc.dialect.oracle;

import com.gitee.melin.bee.core.jdbc.dialect.DefaultTypeConvertor;
import com.gitee.melin.bee.core.jdbc.relational.ConnectionInfo;
import com.gitee.melin.bee.core.jdbc.enums.DataSourceType;
import com.gitee.melin.bee.core.jdbc.dialect.DataTypeConvertor;
import com.gitee.melin.bee.core.jdbc.dialect.AbstractJdbcDialect;
import com.gitee.melin.bee.core.jdbc.dialect.IDBQuery;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.List;

public class OracleJdbcDialect extends AbstractJdbcDialect {

    @Override
    public DataSourceType getDataSourceType() {
        return DataSourceType.ORACLE;
    }

    public OracleJdbcDialect(ConnectionInfo connectionInfo) {
        super(connectionInfo);
    }

    public OracleJdbcDialect(Connection connection) {
        super(connection);
    }

    @Override
    protected IDBQuery getDBQuery() {
        return new OracleQuery();
    }

    @Override
    protected DataTypeConvertor getTypeConvert() {
        return new DefaultTypeConvertor();
    }

    @Override
    protected String testConnectionSql() {
        return "select 1 from dual";
    }

    @Override
    protected Driver getDriver() throws SQLException {
        return loadDriver(DataSourceType.ORACLE.getDriverClass());
    }

    @Override
    public List<String> getDatabases() {
        return this.getSchemas();
    }

    @Override
    protected boolean filterSchema(String schemaName) {
        if (schemaName.equalsIgnoreCase("sys")
                || schemaName.equalsIgnoreCase("system")
                || schemaName.equalsIgnoreCase("xdb")
                || schemaName.equalsIgnoreCase("mdsys")) {
            return false;
        }
        return super.filterSchema(schemaName);
    }

    @Override
    protected String getDDLColumnName() {
        return "ct_script";
    }

    @Override
    protected String createTableSql(String schemaName, String tableName) {
        return "SELECT DBMS_METADATA.GET_DDL('TABLE', '" + tableName + "', '" + schemaName + "') as ct_script FROM DUAL";
    }
}
